<template>
  <div class="task-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>研发项目月度任务管理</span>
          <el-button type="primary" @click="showCreateDialog = true">
            <el-icon><Plus /></el-icon>
            新建任务
          </el-button>
        </div>
      </template>
      
      <el-table :data="tasks" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="title" label="任务标题" width="200" />
        <el-table-column prop="description" label="描述" />
        <el-table-column prop="status" label="状态" width="120">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="priority" label="优先级" width="100">
          <template #default="scope">
            <el-tag :type="getPriorityType(scope.row.priority)">
              {{ scope.row.priority }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="assignee" label="负责人" width="120" />
        <el-table-column prop="deadline" label="截止时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="editTask(scope.row)">
              编辑
            </el-button>
            <el-button size="small" type="danger" @click="deleteTask(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    
    <!-- 创建/编辑任务对话框 -->
    <el-dialog
      :title="editingTask ? '编辑任务' : '新建任务'"
      v-model="showCreateDialog"
      width="600px"
    >
      <el-form :model="taskForm" label-width="100px">
        <el-form-item label="任务标题">
          <el-input v-model="taskForm.title" placeholder="请输入任务标题" />
        </el-form-item>
        <el-form-item label="任务描述">
          <el-input
            v-model="taskForm.description"
            type="textarea"
            :rows="3"
            placeholder="请输入任务描述"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="taskForm.status" placeholder="请选择状态">
            <el-option label="待办" value="TODO" />
            <el-option label="进行中" value="IN_PROGRESS" />
            <el-option label="已完成" value="DONE" />
          </el-select>
        </el-form-item>
        <el-form-item label="优先级">
          <el-select v-model="taskForm.priority" placeholder="请选择优先级">
            <el-option label="高" value="HIGH" />
            <el-option label="中" value="MEDIUM" />
            <el-option label="低" value="LOW" />
          </el-select>
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="taskForm.assignee" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="截止时间">
          <el-date-picker
            v-model="taskForm.deadline"
            type="datetime"
            placeholder="选择截止时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="showCreateDialog = false">取消</el-button>
        <el-button type="primary" @click="saveTask">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { taskApi } from '../api/taskApi'
import { ElMessage, ElMessageBox } from 'element-plus'

export default {
  name: 'TaskList',
  data() {
    return {
      tasks: [],
      loading: false,
      showCreateDialog: false,
      editingTask: null,
      taskForm: {
        title: '',
        description: '',
        status: 'TODO',
        priority: 'MEDIUM',
        assignee: '',
        deadline: ''
      }
    }
  },
  mounted() {
    this.loadTasks()
  },
  methods: {
    async loadTasks() {
      this.loading = true
      try {
        const response = await taskApi.getAllTasks()
        if (response.code === 200) {
          this.tasks = response.data
        } else {
          ElMessage.error(response.message)
        }
      } catch (error) {
        ElMessage.error('加载任务列表失败')
        console.error(error)
      } finally {
        this.loading = false
      }
    },
    
    editTask(task) {
      this.editingTask = task
      this.taskForm = { ...task }
      this.showCreateDialog = true
    },
    
    async saveTask() {
      try {
        let response
        if (this.editingTask) {
          response = await taskApi.updateTask(this.editingTask.id, this.taskForm)
        } else {
          response = await taskApi.createTask(this.taskForm)
        }
        
        if (response.code === 200) {
          ElMessage.success(this.editingTask ? '更新成功' : '创建成功')
          this.showCreateDialog = false
          this.resetForm()
          this.loadTasks()
        } else {
          ElMessage.error(response.message)
        }
      } catch (error) {
        ElMessage.error('操作失败')
        console.error(error)
      }
    },
    
    async deleteTask(id) {
      try {
        await ElMessageBox.confirm('确定要删除这个任务吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        
        const response = await taskApi.deleteTask(id)
        if (response.code === 200) {
          ElMessage.success('删除成功')
          this.loadTasks()
        } else {
          ElMessage.error(response.message)
        }
      } catch (error) {
        if (error !== 'cancel') {
          ElMessage.error('删除失败')
          console.error(error)
        }
      }
    },
    
    resetForm() {
      this.editingTask = null
      this.taskForm = {
        title: '',
        description: '',
        status: 'TODO',
        priority: 'MEDIUM',
        assignee: '',
        deadline: ''
      }
    },
    
    getStatusType(status) {
      const types = {
        'TODO': '',
        'IN_PROGRESS': 'warning',
        'DONE': 'success'
      }
      return types[status] || ''
    },
    
    getStatusText(status) {
      const texts = {
        'TODO': '待办',
        'IN_PROGRESS': '进行中',
        'DONE': '已完成'
      }
      return texts[status] || status
    },
    
    getPriorityType(priority) {
      const types = {
        'HIGH': 'danger',
        'MEDIUM': 'warning',
        'LOW': 'info'
      }
      return types[priority] || ''
    }
  }
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>